package com.amap.api.col.s;

import android.content.Context;
import android.text.TextUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Random;

/* compiled from: StatisticsManager.java */
/* loaded from: classes.dex */
public class eg {
    static boolean a = false;
    static int b = 20;
    private static int c = 20;
    private static WeakReference<eb> d;
    private static int e;

    /* compiled from: StatisticsManager.java */
    /* loaded from: classes.dex */
    public static class a extends ey {
        static int a = 1;
        static int b = 2;
        static int c = 3;
        private Context d;
        private ef f;
        private int g;
        private List<ef> h;

        a(Context context, int i) {
            this.d = context;
            this.g = i;
        }

        a(Context context, int i, List<ef> list) {
            this(context, i);
            this.h = list;
        }

        a(Context context, int i, ef efVar) {
            this(context, i);
            this.f = efVar;
        }

        @Override // com.amap.api.col.s.ey
        public final void a() {
            ef efVar;
            Throwable th;
            ByteArrayOutputStream byteArrayOutputStream;
            int i = this.g;
            if (i == 1) {
                try {
                    if (this.d != null && this.f != null) {
                        synchronized (eg.class) {
                            Context context = this.d;
                            if (context != null && (efVar = this.f) != null) {
                                eg.a(context, efVar.a());
                            }
                        }
                    }
                } catch (Throwable th2) {
                    df.c(th2, "stm", "as");
                }
            } else if (i != 2) {
                if (i == 3) {
                    try {
                        if (this.d == null) {
                            return;
                        }
                        eb a2 = eh.a(eg.d);
                        eh.a(this.d, a2, dd.h, 1000, 307200, "2");
                        if (a2.g == null) {
                            a2.g = new ei(new em(this.d, new ej(new en(new ep()))));
                        }
                        a2.h = 3600000;
                        if (TextUtils.isEmpty(a2.i)) {
                            a2.i = "cKey";
                        }
                        if (a2.f == null) {
                            a2.f = new et(this.d, a2.h, a2.i, new eq(a2.a, new er(this.d, eg.a, eg.c * 1024, eg.b * 1024, "staticUpdate", eg.e * 1024)));
                        }
                        ec.a(a2);
                    } catch (Throwable th3) {
                        df.c(th3, "stm", "usd");
                    }
                }
            } else {
                try {
                    synchronized (eg.class) {
                        if (this.h != null && this.d != null) {
                            ByteArrayOutputStream byteArrayOutputStream2 = null;
                            byte[] bArr = new byte[0];
                            try {
                                byteArrayOutputStream = new ByteArrayOutputStream();
                            } catch (Throwable th4) {
                                th = th4;
                            }
                            try {
                                for (ef efVar2 : this.h) {
                                    if (efVar2 != null) {
                                        byteArrayOutputStream.write(efVar2.a());
                                    }
                                }
                                bArr = byteArrayOutputStream.toByteArray();
                                try {
                                    byteArrayOutputStream.close();
                                } catch (Throwable th5) {
                                    th = th5;
                                    th.printStackTrace();
                                    eg.a(this.d, bArr);
                                }
                            } catch (Throwable th6) {
                                th = th6;
                                byteArrayOutputStream2 = byteArrayOutputStream;
                                df.c(th, "stm", "aStB");
                                if (byteArrayOutputStream2 != null) {
                                    try {
                                        byteArrayOutputStream2.close();
                                    } catch (Throwable th7) {
                                        th = th7;
                                        th.printStackTrace();
                                        eg.a(this.d, bArr);
                                    }
                                }
                                eg.a(this.d, bArr);
                            }
                            eg.a(this.d, bArr);
                        }
                    }
                } catch (Throwable th8) {
                    df.c(th8, "stm", "apb");
                }
            }
        }
    }

    public static synchronized void a(ef efVar, Context context) {
        synchronized (eg.class) {
            ex.a().b(new a(context, a.a, efVar));
        }
    }

    public static synchronized void a(List<ef> list, Context context) {
        synchronized (eg.class) {
            if (list != null) {
                try {
                    if (list.size() != 0) {
                        ex.a().b(new a(context, a.b, list));
                    }
                } catch (Throwable unused) {
                }
            }
        }
    }

    public static void a(Context context) {
        ex.a().b(new a(context, a.c));
    }

    static /* synthetic */ void a(Context context, byte[] bArr) throws IOException {
        eb a2 = eh.a(d);
        eh.a(context, a2, dd.h, 1000, 307200, "2");
        if (a2.e == null) {
            a2.e = new dj();
        }
        Random random = new Random();
        try {
            ec.a(Integer.toString(random.nextInt(100)) + Long.toString(System.nanoTime()), bArr, a2);
        } catch (Throwable th) {
            df.c(th, "stm", "wts");
        }
    }
}
